Skip to content

Stop Entry.new and the serialisers from mutating the caller's hash - #202

Merged
tmaier merged 2 commits into
masterfrom
fix-entry-new-mutation
Nov 14, 2026
Merged

tmaier merged 2 commits into
masterfrom
fix-entry-new-mutation

Conversation

@tmaier

@tmaier tmaier commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

Follow-up to #201, which stopped Bibliography#save_to from changing the options hash passed to it. This PR fixes the same kind of bug in Entry.new and in the serialisers.

Bug

Entry#initialize removed :bibtex_type and :bibtex_key from the attribute hash with Hash#delete, so the caller's hash lost both keys, and a frozen hash raised FrozenError:

ATTRS = { bibtex_type: :book, bibtex_key: 'rails', title: 'x' }.freeze
BibTeX::Entry.new(ATTRS)  # FrozenError: can't modify frozen Hash
h = { bibtex_type: :book, bibtex_key: 'k', title: 't' }
BibTeX::Entry.new(h); h   # => { title: 't' }

test_construct worked around this with RAILS_ATTRIBUTES.dup.

Fix

  • Entry.new goes through the hash once: it uses :bibtex_type and :bibtex_key to set the type and key, and adds every other pair as a field with Value.create, the same conversion #add uses. The resulting entry is the same as before. Because it no longer calls #add, it also skips the hash copy that #add makes. test_construct now passes the frozen constant directly.
  • Entry#to_s and #to_hash had the same problem: options[:quotes] ||= %w[{ }] wrote into the caller's hash. Bibliography#to_s, #to_a and #to_hash (and so #to_yaml and #to_json) pass their options through to these methods. The entry methods now merge the default into a new hash. Bibliography#to_s and #to_a merge it once up front, so the entries don't each copy the hash. #to_hash now calls #to_a.

Tests cover a frozen hash and an unchanged caller hash, including the type and key keys, for Entry.new, Entry#to_s, Entry#to_hash and Bibliography#to_s, #to_a and #to_hash. Another test checks that the resulting entry's type, key and fields have not changed.

Other places checked

No change needed:

  • Entry#add builds a new hash with Hash[*arguments.flatten].
  • Bibliography#add and << with a hash go through Element.parse to Entry.new, which this PR fixes.
  • Bibliography.new and Lexer.new use defaults.merge(options).
  • CiteProcConverter uses { quotes: [] }.merge(options).
  • BibTeXMLConverter only reads its options.
  • Value#to_s uses reject.
  • Names only reads its options.
  • RDFConverter#remove_from_fallback deletes from its own copy of fields.keys.

Open question: Bibliography#to_s and #to_a now pass :quotes to every element, not only to entries. All the other element types ignore their options, so the output is the same.

No callers in lib/, test/ or features/ depended on the keys being removed.

Allocations

Measured with GC.stat(:total_allocated_objects) on 1000 generated @book entries with five fields each:

Ruby 3.4.12 before → after Ruby 4.0.6 before → after
BibTeX.parse 229,022 → 226,022 225,022 → 222,022
Bibliography#to_s 56,023 → 56,026 46,023 → 46,026
Bibliography#to_a 38,008 → 38,010 32,008 → 32,010
1000 × Entry.new (type, key, title) 17,992 → 14,993 16,993 → 13,994

Parsing allocates three objects fewer per entry. Serialising allocates two or three more per call, not per entry. Without the up-front merge in Bibliography, it would have been about 3,000 more per 1000 entries.

rake test and cucumber pass on Ruby 3.4.12 and 4.0.6: 916 runs with 0 failures, and 141 of 141 scenarios. rubocop reports no offenses, and .rubocop_todo.yml is unchanged.

Entry#initialize took :bibtex_type and :bibtex_key out of the attribute
hash with Hash#delete, so the caller's hash lost both keys and a frozen
hash raised FrozenError. It now reads the two keys while it walks the
hash and adds every other pair as a field, which leaves the argument
alone and builds the same entry.

Walking the hash directly also skips the copy that #add makes, so
parsing 1000 entries allocates 3000 fewer objects.

test_construct can pass the frozen RAILS_ATTRIBUTES without a dup.
Both set the default quotes with options[:quotes] ||= ..., which wrote
into the caller's hash and raised FrozenError for a frozen one.
Bibliography#to_s, #to_a and #to_hash pass their options straight
through, so they had the same problem.

The entry methods now merge the default into a new hash when :quotes
is missing. Bibliography#to_s and #to_a do the same once up front, so
the entries see :quotes and do not each copy the hash; serialising
1000 entries allocates three objects more than before, not 3000.
Copilot AI balanced review requested due to automatic review settings November 14, 2026 22:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@tmaier
tmaier merged commit c16f0ab into master Nov 14, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants